package com.atguigu.service.impl;

import com.atguigu.dao.BaseDao;
import com.atguigu.dao.CommunityDao;
import com.atguigu.dao.DictDao;
import com.atguigu.entity.Community;
import com.atguigu.entity.Dict;
import com.atguigu.service.CommunityService;
import com.atguigu.service.DictService;
import com.atguigu.util.CastUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

@DubboService
public class CommunityServiceImpl extends BaseServiceImpl<Community> implements CommunityService {
    @Autowired
    private CommunityDao communityDao;
    @Autowired
    private DictDao dictDao;

    @Override
    public BaseDao<Community> getBaseDao() {

        return communityDao;
    }

    @Override
    public PageInfo<Community> findPage(Map filters) {
        int pageNum = CastUtil.castInt(filters.get("pageNum"));
        int pageSize = CastUtil.castInt(filters.get("pageSize"));
        PageHelper.startPage(pageNum, pageSize);
        List<Community> list = getBaseDao().findPage(filters);
        for (Community community : list) {
            Dict areaDict = dictDao.findById(community.getAreaId());
            community.setAreaName(areaDict.getName());
            Dict plateDict = dictDao.findById(community.getPlateId());
            community.setPlateName(plateDict.getName());
        }
        return new PageInfo<>(list,3);
    }

    @Override
    public Community getById(Serializable id) {
        Community community = communityDao.getById(id);
        community.setAreaName(dictDao.findById(community.getAreaId()).getName());
        community.setPlateName(dictDao.findById(community.getPlateId()).getName());
        return community;
    }

    @Override
    public List<Community> getAll() {
        return communityDao.getAll();
    }
}
